package tools.jsonparse;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class GSONParse implements Parse {

	@Override
	public <T> String beanToStr(T t) {
		Gson gson = new Gson();
		String jsonObject = gson.toJson(t);
		return jsonObject;
	}

	@Override
	public <T> T strToBean(String str, Class<T> tClass) {
		Gson gson = new Gson();
		T t=gson.fromJson(str, tClass);
		return t;
	}

	@Override
	public String strToPretty(String str) {
		JsonParser parser = new JsonParser();
		JsonObject json = parser.parse(str).getAsJsonObject();
		Gson gson = new GsonBuilder().setPrettyPrinting().create();
		String prettyJson = gson.toJson(json);
		return prettyJson;
	}

}
